using System;
using System.Web.Mvc;
using System.Web.Routing;
using ContactCenter.Core.Common;
using StructureMap;

namespace ContactCenter.UI.Web.Extensibility
{
    public class MyAppControllerFactory : DefaultControllerFactory
    {
        private readonly IContainer _container;

        public MyAppControllerFactory(IContainer container)
        {
            _container = container;
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if(controllerType.IsNull())
            {
                return base.GetControllerInstance(requestContext, controllerType);
            }

            var controller = _container.GetInstance(controllerType) as IController;
            return controller.IsNull() ? base.GetControllerInstance(requestContext, controllerType) : controller;
        }
    }
}